package com.guigu.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.R;
import com.guigu.eduservice.entity.SellerQualification;
import com.guigu.eduservice.service.SellerQualificationService;
import com.guigu.eduservice.vo.SellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 卖家资质表（存储卖家实名认证材料及审核结果） 前端控制器
 * </p>
 *
 * @author janecraft
 * @since 2025-09-27
 */
@RestController
@RequestMapping("/eduservice/sellerQualification")
public class SellerQualificationController {
    @Autowired
    private SellerQualificationService sellerQualificationService;

    @GetMapping("/getUseId/{useId}")
    public SellerQualification getUseId(@PathVariable("useId") Long useId) {
        LambdaQueryWrapper<SellerQualification> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SellerQualification::getUserId, useId);
        wrapper.eq(SellerQualification::getAuditStatus, 1);
        return sellerQualificationService.getOne(wrapper);
    }
    @GetMapping("/getUseId2/{useId}")
    public SellerQualification getUseId2(@PathVariable("useId") Long useId) {
        LambdaQueryWrapper<SellerQualification> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SellerQualification::getUserId, useId);
        return sellerQualificationService.getOne(wrapper);
    }

    @PostMapping("/addShangHu")
    public R addShangHu(@RequestBody SellerQualification sellerQualification) {

        if (sellerQualification.getUserId() == null){
            return R.error().message("用户ID不能为空");
        }
            //查询申请是否存在，如果存在就修改不存在就添加
            LambdaQueryWrapper<SellerQualification> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SellerQualification::getUserId, sellerQualification.getUserId());
            SellerQualification sellerQualification1 = sellerQualificationService.getOne(wrapper);
            boolean save = false;
            if (sellerQualification1 != null){
               save = sellerQualificationService.update(sellerQualification,wrapper);
            }else {
                save = sellerQualificationService.save(sellerQualification);
            }
       return save ? R.ok() : R.error();
    }


    @PostMapping("/page/{pageNo}/{pageSize}")
    public PageInfo<SellerQualification> page(@PathVariable("pageNo") Integer pageNo,
                                              @PathVariable("pageSize") Integer pageSize,
                                              @RequestBody SellerVo vo) {
        return sellerQualificationService.page(pageNo, pageSize,vo);
    }

    @PutMapping("/updateAudit")
    public boolean updateAudit(@RequestBody SellerVo vo) {
        return sellerQualificationService.updateAudit(vo);
    }
}
